package com.Sep_Two;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * 199. 二叉树的右视图
 * 给定一个二叉树的 根节点 root，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。
 */

public class Solution199 {
    public List<Integer> rightSideView(TreeNode root){
        List<Integer> res=new ArrayList<>();
        if (root==null) return res;
        Deque<TreeNode> deque=new LinkedList<>();
        deque.offer(root);
        while (!deque.isEmpty())
        {
            int size=deque.size();
            for (int i=0;i<size;i++)
            {
                TreeNode treeNode=deque.pollFirst();
                if (treeNode.left!=null) deque.offer(treeNode.left);
                if (treeNode.right!=null) deque.offer(treeNode.right);
                if (i==size-1)
                {
                    res.add(treeNode.val);
                }
            }
        }
        return res;
    }

      public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
    }
}
